package ysq.xyg.web.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.web.dao.WebVideoDao;
import ysq.xyg.web.model.po.WebVideo;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.web.model.vo.WebVideoVo;
import ysq.xyg.web.service.WebVideoService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 业务模块-视频管理service实现
 *
 * @author shengQiang yu
 * @date 2019-7-7 15:49:32
 */
@Service("webVideoService")
public class WebVideoServiceImpl extends BaseServiceImpl<WebVideo> implements WebVideoService {

    @Resource
    private WebVideoDao webVideoDao;

    private WebVideo webVideo;

    @Autowired
    public void setBaseDao(WebVideoDao webVideoDao) {
        this.baseDao = webVideoDao;
    }


    /**
     * 分页查询
     *
     * @param webVideoVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-7 15:49:32
     */
    public ReturnEntity page(WebVideoVo webVideoVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<WebVideo> list = acceptList(webVideoVo);
        PageInfo<WebVideo> page=new PageInfo<WebVideo>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param webVideoVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-7 15:49:32
     */
    public ReturnEntity addOrUpdate(WebVideoVo webVideoVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(webVideoVo.getId())){
            webVideo=new WebVideo(currentUser);
            isUpate=false;
        }else{
            webVideo=selectByPrimaryKey(webVideoVo.getId());
            if(ObjectUtil.isNull(webVideo)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            webVideo.setUpdateTime(new Date());
            webVideo.setUpdateUser(currentUser.getId());
        }
        webVideo.setTitle(webVideoVo.getTitle());
        webVideo.setPicture(webVideoVo.getPicture());
        webVideo.setVideoType(webVideoVo.getVideoType());
        webVideo.setUrl(webVideoVo.getUrl());
        webVideo.setDescript(webVideoVo.getDescript());
        webVideo.setStatus(webVideoVo.getStatus());
        webVideo.setSort(webVideoVo.getSort());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(webVideo);
        }else{
            num=insert(webVideo);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-7 15:49:32
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        WebVideo webVideo=selectByPrimaryKey(id);
        if(webVideo!=null){
            webVideo.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            webVideo.setUpdateTime(new Date());
            webVideo.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(webVideo);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param webVideoVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-7-7 15:49:32
     */
    public List<WebVideo> acceptList(WebVideoVo webVideoVo){
        Example example = new Example(WebVideo.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(webVideoVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",webVideoVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(webVideoVo.getTitle())){
            criteria.andEqualTo("title","%"+webVideoVo.getTitle()+"%");
        }
        if(StringUtil.isNotEmpty(webVideoVo.getVideoType())){
            criteria.andEqualTo("videoType",webVideoVo.getVideoType());
        }
        if(StringUtil.isNotEmpty(webVideoVo.getStatus())){
            criteria.andEqualTo("status",webVideoVo.getStatus());
        }
        example.setOrderByClause(" sort desc,update_time desc");
        return webVideoDao.selectByExample(example);
    }
}
